package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000121626621";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCF49F/cOJv/MY3ma/T2DP7DGaUO3M4N6NFmE436kMw7JJLr9V5lz0V+IgGY+n+Wilgtd09UC1ykHvIj4/FCCIQjZ3qXA9kpgTki86FGjbceIuTGoVlveeTWHyHLgSnJ5ZXYRfhyK7iGWKQXPilba5l5tNgl1YSGMmOIKkxwWrV6BMN6sqmgIRBM0jI/zMlcpXH8qbxkBkIWV8uHmYvGl3TnRH9CtTLq0wA2s1hmETRF3soCGgd2W2UXs6kCxPOFMOm75APllB/+9t/WME8tg4Cy8g1/X6OLSljbkcVhq58O3izXlONeeB0SG4mNsi83VJ03JFwdV0XjNkY0MxT5PT9AgMBAAECggEAUlocI5zVSjhZ2x0GjvCumD26YnZmmi9ul4HkqnPGvDLmkPMoBM/HXNovIG8YH8BpLvjtiablLVA9MtFKhtBH05KnjNzvAuMyozhy4F+NWvIyN1rargeuhmyou6TMa9vnlvs7az66gotGmrD0pxvdZwQhA4pSAHOKlYb1SotDISnhwCkWDiiBV24yOf8aWCAunlQIXmhb1XpC/u10bBoQ7qAFPHzvMWmq4g8A4zh3yFGOCoMpIClr20fHKwrSKkzmMuFzuqW4Zbf7+kc1Bc4nCessiw+xe3Ga/E1lYtNOTbfMVQsK/+2eNmYlczh5Oa5peDtnOHWxLBGH7u2f8ydUwQKBgQDow67vvY4GwJY7cf4ib6+q9l1/5wIYmTC7dKV6Nf0p3jrqgKaqQfQIGphjUFPR824r2zRBJmgw0KdiSv+mt7enzaYnbcQtMpcjfUHEC3YdHhrHQFvYb1WGQDM1LhOsN4TkbL8zTM1s6h98al5AMAlXqL6fTfzfJ54pW99auIYd6QKBgQCTQWNhieKakGEKFkgAV7P2ebjGpXIVBkTFP8NbaB9kzeniT1m2/Vhu+OFx0GESV3k7aLa2WJsWwcnGLkdOay2QTFB3h8lAvENQyykk3Zo+ZdbtfIvCutlF5WJoncNaDhvI8aOvgeRp3Gm5I76/E7IPJ2KGad+BAzV45wn3BOmN9QKBgQDkBA2yvLYM10xUH28cvNwRVhZx3RKoliiu+a3A8nJ2OUTtw+mAj+tGpQbzAxWZbiZWv/2IdN9MMppGJ8IYx5AZydMifNzYid6U3Xc3XSLvkzYYSmuusiHNycZgKisLYbfFLOKAlpQGRbNiZbC+oFAGlz9lPzigQmcljOJYNLrtQQKBgGFR+M1w5IuEWi3hdeqMDAscfSsEEvakMvUt4/3YPc9Qa3+/M6rFmxmUcOLYdh86IoxJ819mBfGvV5wb39KK8Q0J8rBc1HxtMAwIiQOtVZBhm/9Orcuf05LnO/eqNH6jDmfqk2Sd8winvFO4tQrxiF+NhwcQl79bEI78GtDftT+1AoGALd+Q6om+wib0fpwzsnVMvTMJrLNOWSsX/y7zgX0KNNYnWfj5d4I0318vIl9OAg9luTCl8N282V844UvdP0EbIelkj4qOBA/kaT68LiRxA9aF5KzGiLuqZCz2HA6+s5P5MXqOU/nHD3UfFzHvKJb4j3mLXKj3K3pERyKtmYj9gA4=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg7g52b1j/2oLtPTGx7pwkBGUY3h7X6wcgA68LQ3b1Kjti59HIOE0A4LEShCkMVPdr1N6CmmeUz7qTBghaZCqt/6kuZDf+PutDNVTAmq1TVor+CeVL1yDmQLSJUKSgbK1BUiKAzfkWeaYl4qnnFynb3vpjRx5LZPJV+mMMqF4yDG5KzPMt8NkgViRrBSxeQOqCAI/+wW0k1keimYYaC5kMP4UJV7oikztXtcOIWnUUU0Ix8UxaPVx70QBDaBRy2gX7s3t/uqQ1Bz7KJD5AWdQ+MPvnOW/g5QQ/uLmNpXtKPHbQDLEyczRqJNQLZ7yTghJcBUvygz6ZdxkjTH0CpZ1YQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://dcm3z6.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 订单超时时间,到指定时间后,支付宝会自动关闭支付窗口
    private String timeout = "1m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
// 用time_expire，别用timeout_express
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
